<template>
  <div class="login-container">
    <el-card class="login-card">
      <div class="login-header">
        <img src="@/assets/img/logo.png" alt="Logo" class="logo">
        <h1 class="title">欢迎登录</h1>
      </div>
      <el-form :model="loginForm" :rules="rules" ref="loginFormRef" label-position="top">
        <el-form-item prop="username" label="用户名">
          <el-input 
            v-model="loginForm.username" 
            prefix-icon="el-icon-user"
            placeholder="请输入您的用户名"
            @keyup.enter="submitForm('loginForm')">
          </el-input>
        </el-form-item>
        <el-form-item prop="password" label="密码">
          <el-input 
            v-model="loginForm.password" 
            prefix-icon="el-icon-lock"
            type="password" 
            placeholder="请输入您的密码"
            @keyup.enter="submitForm('loginForm')">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('loginForm')" class="login-button">
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { login } from '@/api/admin'
import { useAdminStore } from '@/store/modules/admin'

const router = useRouter()
const adminStore = useAdminStore()

const loginFormRef = ref(null)

const loginForm = ref({
  username: '',
  password: ''
})
console.log(loginForm.value)
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ]
}

const submitForm = async (formName) => {
  if (!loginFormRef.value) return
  
  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      try {
       const res = await login(loginForm.value)
       console.log(res)
          if(res.code===200){
          adminStore.setToken(res.data.token)
          adminStore.setAdminInfo(res.data.administrator)
          localStorage.setItem('token',res.data.token)
          localStorage.setItem('admin',JSON.stringify(res.data.administrator))
          ElMessage.success('登录成功')
          router.push('/admin/')
          }else{
            ElMessage.error('登录失败')
          }
      } catch (error) {
        ElMessage.error('登录失败：' + error.message)
      }
    }
  })
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: right;
  align-items: center;
  height: 100vh;
  background: url("@/assets/img/login.png") no-repeat center center;
}

.login-card {
  width: 100%;
  max-width: 400px;
  padding: 2rem;
  border-radius: 8px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1), 0 1px 3px rgba(0, 0, 0, 0.08);
  margin-right: 100px;
}

.login-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 2rem;
}

.logo {
  width: 60px;
  height: 60px;
  margin-bottom: 1rem;
  border-radius: 50%;
}

.title {
  font-size: 1.5rem;
  color: #303133;
  margin: 0;
}

.login-button {
  width: 100%;
  padding: 12px 20px;
  font-size: 16px;
}

.additional-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 1rem;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-card {
    max-width: 90%;
    padding: 1.5rem;
  }
}
</style> 